package com.hbvtc.xxxy.nikeSale.frames;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.hbvtc.xxxy.nikeSale.daos.GoodsDao;
import com.hbvtc.xxxy.nikeSale.daos.SalesmanDao;
import com.hbvtc.xxxy.nikeSale.models.Salesman;

public class GoodsShopNike extends JFrame {
	public GoodsShopNike() {
		this.setTitle("欢迎来到注册界面");
		this.setSize(600, 400);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		// 设置标题栏的图标
		ImageIcon imageIcon = new ImageIcon(getClass().getResource("01.png"));
		this.setIconImage(imageIcon.getImage());

		// 背景图片的路径
		String path = "images/09.png";
		ImageIcon background = new ImageIcon(path);
		// 把背景图片显示在一个标签里面
		JLabel label = new JLabel(background);
		// 把标签的大小位置设置为图片刚好填充整个面板
		label.setBounds(0, 0, this.getWidth(), this.getHeight());
		// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
		JPanel imagePanel = (JPanel) this.getContentPane();
		imagePanel.setOpaque(false);
		// 把背景图片添加到分层窗格的最底层作为背景
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		//this.setVisible(true);
		//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//直接关闭整个系统

		this.setLayout(null);
		 JLabel lalNameId = new JLabel("请输入用  户  名：");
		 JLabel lalPwd = new JLabel("请输入密        码：");
		 JLabel lalName = new JLabel("请输入姓        名： ");
		 JLabel lalTel = new JLabel("请输入手机号码： ");

		final JTextField txtNameId = new JTextField();
		final JTextField txtPwd = new JTextField();
		final JTextField txtName = new JTextField();
		final JTextField txtTel = new JTextField();

		JButton btnLog = new JButton("注册");
	    final JButton btnBack = new JButton("返  回");

		this.add(lalNameId);
		this.add(lalPwd);
		this.add(lalName);
		this.add(lalTel);
		this.add(txtNameId);
		this.add(txtPwd);
		this.add(txtName);
		this.add(txtTel);
		this.add(btnLog);
		this.add(btnBack);

		lalNameId.setBounds(50, 10, 200, 60);
		lalNameId.setFont(new Font(null, 0, 22));
		lalPwd.setBounds(50, 60, 200, 60);
		lalPwd.setFont(new Font(null, 0, 22));
		lalName.setBounds(50, 110, 200, 60);
		lalName.setFont(new Font(null, 0, 22));
		lalTel.setBounds(50, 160, 200, 60);
		lalTel.setFont(new Font(null, 0, 22));

		txtNameId.setBounds(240, 20, 300, 40);
		txtNameId.setFont(new Font(null, 0, 22));
		txtPwd.setBounds(240, 70, 300, 40);
		txtPwd.setFont(new Font(null, 0, 22));
		txtName.setBounds(240, 120, 300, 40);
		txtName.setFont(new Font(null, 0, 22));
		txtTel.setBounds(240, 170, 300, 40);
		txtTel.setFont(new Font(null, 0, 22));
		btnLog.setBounds(100, 250, 150, 50);
		btnLog.setFont(new Font(null, 0, 22));
		btnBack.setBounds(350, 250, 150, 50);
		btnBack.setFont(new Font(null, 0, 22));
		btnBack.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dispose();
				new MainFramesNike();
			}
		});
		final Salesman salesman=new Salesman();

		btnLog.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int id = Integer.parseInt(txtNameId.getText().trim());
				String pwd = txtPwd.getText();
				String name = txtName.getText();
				String mobile = txtTel.getText();
				try {
					salesman.setSalesmanId(id);
					salesman.setPwd(pwd);
					salesman.setSalesmanName(name);
					salesman.setMobile(mobile);
					GoodsDao goodsDao = new GoodsDao();
					try {
						if (goodsDao.getBySalesmanId(id)== true) {
							SalesmanDao.addUser(salesman);
							JOptionPane.showMessageDialog(GoodsShopNike.this, "注册成功");
						} else {
							JOptionPane.showMessageDialog(GoodsShopNike.this, "用户名重复");
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				} catch (NumberFormatException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
			}
		});
	}

	public static void main(String[] args) {
		new GoodsShopNike().setVisible(true);
	}
}
